<template>
  <div class="components-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:leaderboard:add']"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5"> </el-col>

      <right-toolbar @queryTable="getgonggao_list"></right-toolbar>
    </el-row>
    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="ID" width="180"> </el-table-column>
        <el-table-column prop="title" label="标题" width="180">
        </el-table-column>
        <el-table-column prop="name" label="公告轮播" width="180">
        </el-table-column>
        <el-table-column prop="content" label="详情" width="580">
          <template slot-scope="scope">
            <div style="height: 100px" v-html="scope.row.content" />
          </template>
        </el-table-column>
        <el-table-column prop="adminId" label="创建员" width="180">
        </el-table-column>
        <el-table-column prop="creator" label="创建时间"> </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleLookTable(scope.row, 1)"
              v-hasPermi="['system:list:edit']"
              >修改</el-button
            >

            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:list:remove']"
              >删除</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="edil_name" :visible.sync="dialogTableVisible">
      <div>
        <el-form label-width="80px" :model="form">
          <el-form-item label="标题">
            <el-input v-model="form.title"></el-input>
          </el-form-item>
          <el-form-item label="公告主体内容">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
        </el-form>
      </div>

      <el-upload
        :action="action"
        :headers="headers"
        name="files"
        :on-success="successHandle"
        :on-preview="handlePreview"
      >
        <el-button icon="el-icon-thumb" size="small" type="primary"
          >获取图片url</el-button
        >
      </el-upload>

      <!-- <span>
        {{url}}
      </span> -->
      <el-alert
        :closable="false"
        :title="url"
        type="success"
        @click="copy(url)"
      />

      <div class="editor-container">
        <markdown-editor
          :content="content"
          ref="markdownEditor"
          @changeMsg="getHtml"
        />

        <!-- <div v-html="html" /> -->
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getgonggao_list,
  deligonggao_list,
  putgonggao_list,
} from "@/api/jiemian/index.js";
import MarkdownEditor from "@/components/MarkdownEditor";
import { getToken, getEnv, setEnv } from "@/utils/auth";
export default {
  name: "MarkdownDemo",
  components: { MarkdownEditor },
  data() {
    return {
      dialogTableVisible: false,
      tableData: [],
      form: {
        id: "",
        title: "",
        name: "",
        content: "",
        adminId: 1,
        creator: this.$store.getters.id,
      },
      content: "fsdfdsf",
      edil_name: "",
      url: "",
      token: getToken(),
    };
  },
  computed: {
    action() {
      if(getEnv()=='1'){
        return  process.env.VUE_APP_BASE_API+'/system/admin/uploadFile'
      }else{
         return process.env.VUE_APP_TEST_API+"/system/admin/uploadFile";
      }
     
    },
    headers() {
      return {
        Authorization: this.token,
      };
    },
  },
  methods: {
    successHandle(response) {
      if (response && response.code == 200) {
        console.log(response);
        this.url = response.data[0];
      } else {
        this.$message.error(response.message);
      }
    },

    /** 查看商品 */
    handleLookTable(row, type) {
      this.dialogTableVisible = true;
      const tableId = row.id || 0;
      const tableName = row.title || 0;
      //   const params = { pageNum: this.queryParams.pageNum };
      console.log(row);

      this.edil_name = row.title;
      this.form = row;
      this.content = this.form.content;
      this.$refs.text = this.form.content;
      console.log(this.$refs.text);
      // this.$tab.openPage(
      //   "查看[" + tableName + "]公告",
      //   "/gonggao/add/index/"+tableId
      // );
    },

    //新增公告

    handleAdd() {
      this.dialogTableVisible = true;
      this.edil_name = "新增公告";
      this.form = {
        id: "",
        title: "",
        name: "",
        content: "",
        adminId: 1,
        creator: this.$store.getters.id,
      };
      this.content = "";
      this.$refs.text = "";
    },
    getHtml(e) {
      console.log(e);
      let data = {
        id: this.form.id,
        title: this.form.title,
        name: this.form.name,
        content: e,
        adminId: 1,
        creator: 1,
      };
      if (data.id) {
        deligonggao_list(data).then((res) => {
          console.log(res);
          this.getgonggao_list();
          this.dialogTableVisible = false;
        });
      } else {
        putgonggao_list(data).then((res) => {
          console.log(res);
          this.getgonggao_list();
          this.dialogTableVisible = false;
        });
      }
    },
    //获取公告列表
    getgonggao_list() {
      getgonggao_list().then((res) => {
        console.log(res.rows);
        this.tableData = res.rows;
      });
    },
  },
  created() {
    this.getgonggao_list();
  },
};
</script>

<style scoped>
.editor-container {
  margin-bottom: 30px;
}
.tag-title {
  margin-bottom: 5px;
}
</style>
